package fs

import (
	"fmt"
	"os"
	"time"
)

type Void struct{}

func Ls(sc <-chan Entry) {
	for fs := range sc {
		d := fs.Info
		fmt.Printf("%s %d %s %s\n",
			modes(d), d.Size(), fileTimeString(d.ModTime()), fs.Path)
	}
}

func fileTimeString(t time.Time) string {
	return t.Format(time.ANSIC)
}

var mtab = []string{
	"---", "--x", "-w-", "-wx",
	"r--", "r-x", "rw-", "rwx",
}

func modes(d os.FileInfo) string {
	buf := make([]byte, 2+3*3)
	mode := d.Mode()
	switch {
	case d.IsDir():
		buf[0] = 'd'
	case (mode & os.ModeSymlink) != 0:
		buf[0] = 'L'
	case (mode & os.ModeType) == 0:
		buf[0] = '-'
	default:
		buf[0] = '?'
	}
	buf[1] = '-'
	copy(buf[2+0*3:], mtab[(mode>>6)&7])
	copy(buf[2+1*3:], mtab[(mode>>3)&7])
	copy(buf[2+2*3:], mtab[(mode>>0)&7])
	return string(buf)
}
